package cn.htsyb.synergy.utils;
/**
 * 
 * @author lakeside hubin_yuan.410@hotmail.com
 * @version V1.0.0
 * @since 1.0
 * @date 2022/5/11
 */
public class CharUtil {
    public static final char[] CHARACTER = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'G', 'K', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c',
            'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '2', '3', '4', '5', '6', '7', '8', '9'};

    public static final char[] HEX_CHARACTER = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'a', 'b', 'c', 'd', 'e', 'f'};

    /**
     * 首字符大写
     *
     * @param src 目标字符串
     * @return 首字符大写字符串
     */
    public static String upperFirstChar(String src) {
        return src.substring(0, 1).toUpperCase().concat(src.substring(1));
    }

    /**
     * <p>
     * 清除特殊UTF-8字符，解决CXF、MYSQL处理失败问题
     * </p>
     * <p>
     * 清除的字符一般在UTF-8页面无法显示
     * </p>
     *
     * @param content 转换的文本
     * @return
     */
    public static String unescapeUtf8Char(String content) {
        if (content != null) {
            StringBuilder strbuff = new StringBuilder();
            for (int i = 0; i < content.length(); i++) {
                char ch = content.charAt(i);
                if (ch % 0x10000 != 0xffff && // 0xffff - 0x10ffff range
                        ch % 0x10000 != 0xfffe && // 0xfffe - 0x10fffe range
                        (ch <= 0xfdd0 || ch >= 0xfdef) && // 0xfdd0 - 0xfdef
                        // range
                        (ch <= 55295 || ch >= 57344) && // mysql 5.5 before the
                        // version does not
                        // support the character
                        (ch > 0x1F || ch == 0x9 || ch == 0xa || ch == 0xd)) {
                    strbuff.append(ch);
                }
            }
            return strbuff.toString();
        } else {
            return null;
        }
    }

    /**
     * 将字符全部转换为小写后，将字段中指定字符替换掉，并将后续字符首字符大写拼接
     *
     * @param content
     * @param transChar
     * @return
     */

    public static String charTransUpperFirst(String content, String... transChar) {
        if (transChar != null) {
            content = content.toLowerCase();
            String[] sourceSplit = content.split("[" + String.join("|", transChar) + "]");
            StringBuffer strBuff = new StringBuffer();
            for (String splitStr : sourceSplit) {
                strBuff.append(strBuff.length() == 0 ? splitStr : upperFirstChar(splitStr));
            }
            return strBuff.toString();
        } else {
            return content;
        }
    }

}